import 'package:flutter/material.dart';

class DetailPage extends StatefulWidget {
  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  int activeTab = 0;
  bool buttonShow = true;
  bool buttonShow2 = true;
  bool isDialogVisible = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFFF1F1F1), // Approximation of #D1D3D5
      body: SafeArea(
        child: Column(
          children: [
            // Navigation Bar
            buildNavigationBar(),

            // Tabs
            buildTabs(),

            // Token display (this would be handled differently in Flutter)
            buildTokenDisplay(),

            // Main content with tabs
            Expanded(
              child: TabBarView(
                controller: DefaultTabController.of(context),
                children: [
                  // Tab 1: 期望诉求
                  buildExpectationContent(),
                  // Tab 2: 基本信息
                  buildBasicInfoContent(),
                  // Tab 3: 过往信息
                  buildPastInfoContent(),
                  // Tab 4: 经纪人评价
                  buildBrokerEvaluationContent(),
                ],
              ),
            ),

            // Footer buttons
            buildFooterButtons(),
          ],
        ),
      ),
    );
  }

  Widget buildNavigationBar() {
    return Container(
      height: 50,
      padding: EdgeInsets.symmetric(horizontal: 16),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black,
            blurRadius: 2,
            offset: Offset(0, 1),
          ),
        ],
      ),
      child: Row(
        mainAxisAlignment:MainAxisAlignment.spaceBetween,
        children: [
          IconButton(
            icon: Icon(Icons.arrow_back_ios, size: 18),
            onPressed: onClickLeft,
          ),
          Text(
            "详情页",
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
          ),
          SizedBox(width: 40), // Empty space for right icon
        ],
      ),
    );
  }

  Widget buildTabs() {
    return Container(
      height: 45,
      margin: EdgeInsets.only(top: 10),
      child: DefaultTabController(
        length: 4,
        child: TabBar(
          labelColor: Color(0xFF191919),
          unselectedLabelColor: Colors.grey,
          indicatorColor: Color(0xFF191919),
          tabs: [
            Tab(text: "期望诉求"),
            Tab(text: "基本信息"),
            Tab(text: "过往信息"),
            Tab(text: "经纪人评价"),
          ],
          onTap: onClickTab,
        ),
      ),
    );
  }

  Widget buildTokenDisplay() {
    return Container(
      margin: EdgeInsets.only(top: 10, left: 16, right: 16),
      child: Text("token=your_token_value"), // Replace with actual token logic
    );
  }

  Widget buildExpectationContent() {
    return SingleChildScrollView(
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Source info
            buildSourceInfo(),

            // Contract and job info
            buildJobInfo(),

            // Work content
            buildWorkSection(),

            // Time range
            buildTimeRangeSection(),

            // Location
            buildLocationInfo(),

            // Benefits
            buildBenefitsSection(),
          ],
        ),
      ),
    );
  }

  Widget buildSourceInfo() {
    return Container(
      margin: EdgeInsets.only(top: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Image.asset('assets/appimg/laiyuan.png', width: 20, height: 20),
              SizedBox(width: 5),
              Text(
                "来源于平台线下｜C端",
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),
            ],
          ),
          Text(
            "意向度 S",
            style: TextStyle(fontSize: 16),
          ),
        ],
      ),
    );
  }

  Widget buildJobInfo() {
    return Container(
      margin: EdgeInsets.only(top: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "发起定金合同",
            style: TextStyle(fontSize: 16),
          ),
          SizedBox(height: 8),
          Row(
            mainAxisAlignment:MainAxisAlignment.spaceBetween,
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "家员私厨",
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                  ),
                  Text(
                    "5&保姆/保洁",
                    style: TextStyle(fontSize: 14),
                  ),
                ],
              ),
              Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Text(
                    "6000-8000",
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                  ),
                  Text(
                    "/月*14薪",
                    style: TextStyle(fontSize: 12),
                  ),
                ],
              ),
            ],
          ),
          SizedBox(height: 10),
          Text(
            "住家｜8-20点 24小时",
            style: TextStyle(fontSize: 16, color: Color(0xFF191919)),
          ),
        ],
      ),
    );
  }

  Widget buildWorkSection() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "工作内容",
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
        ),
        SizedBox(height: 8),
        buildWorkContent("主要工作：", "接孩子、家庭五口一日三餐、整理房间、清洁房间、开车接孩子上下学、剩下就是产品文档了。接孩子、家庭五口一日三餐、整理房间、清洁房间、开车剩下就是产品文档了。"),
        SizedBox(height: 8),
        buildWorkContent("辅助工作：", "接孩子、家庭五口一日三餐、整理房间、清洁房间、开车接孩子上下学、剩下就是产品文档了。接孩子、家庭五口一日三餐、整理房间、清洁房间、开车剩下就是产品文档了。"),
      ],
    );
  }

  Widget buildTimeRangeSection() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "时间范围",
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
        ),
        SizedBox(height: 8),
        buildTimeContent("面试时间：", "2025.07.02"),
        SizedBox(height: 8),
        buildTimeContent("入户时间：", "2025.07.02"),
      ],
    );
  }

  Widget buildLocationInfo() {
    return Container(
      margin: EdgeInsets.only(top: 10),
      child: Row(
        children: [
          Image.asset('assets/appimg/dingwei.png', width: 20, height: 20),
          SizedBox(width: 5),
          Text(
            "北京市海淀区",
            style: TextStyle(fontSize: 14, color: Color(0xFF191919)),
          ),
        ],
      ),
    );
  }

  Widget buildBenefitsSection() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "福利",
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
        ),
        SizedBox(height: 8),
        // Placeholder for benefits
        Container(
          height: 100,
          color: Colors.grey,
          child: Center(
            child: Text("福利列表 (20项)"),
          ),
        ),
        SizedBox(height: 8),
        Text(
          "其他福利",
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
        ),
      ],
    );
  }

  Widget buildBasicInfoContent() {
    return SingleChildScrollView(
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "基本信息",
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
            ),
            SizedBox(height: 10),
            buildInfoItem("张先生:", "广东人 | 37 | 男 | 狗 | 双鱼座 | 汉族"),
            SizedBox(height: 8),
            buildInfoItem("居住地址:", "希望雇主没有民族歧视、地域歧视。"),
            SizedBox(height: 8),
            buildInfoItem("家庭情况：", "基督教"),
            SizedBox(height: 8),
            buildInfoItem("婚姻状况：", "基督教"),
            SizedBox(height: 8),
            buildInfoItem("老人情况：", "基督教"),
            SizedBox(height: 8),
            buildInfoItem("子女情况：", "基督教"),
            SizedBox(height: 8),
            buildInfoItem("宠物情况：", "基督教"),
          ],
        ),
      ),
    );
  }

  Widget buildPastInfoContent() {
    return SingleChildScrollView(
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "过往信息",
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
            ),
            SizedBox(height: 16),
            buildPastInfoItem("曾聘请过几位服务人员：", "A:3位"),
            SizedBox(height: 16),
            buildPastInfoItem("您认可的服务人员在哪方面最吸引你：", "A:3位"),
            SizedBox(height: 16),
            buildPastInfoItem("与前任服务人员解除合作的原因是什么：", "A:3位"),
            SizedBox(height: 16),
            buildPastInfoItem("分别在那几个结构聘请过服务人员", "A:3位"),
            SizedBox(height: 16),
            buildPastInfoItem("你最忌讳服务人员的那些行为：", "A:3位"),
          ],
        ),
      ),
    );
  }

  Widget buildBrokerEvaluationContent() {
    return SingleChildScrollView(
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "经纪人评价",
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
            ),
            SizedBox(height: 10),
            Text(
              "服务百岁老人真的福气呀，而且老人很喜欢现有白班阿姨，待人亲和、乐观，但新阿姨入府需要有耐心才好被老人认可;XXXXXXXXX整体劳务内容与报酬不错;",
              style: TextStyle(fontSize: 14),
            ),
          ],
        ),
      ),
    );
  }

  Widget buildFooterButtons() {
    return Container(
      height: 70,
      padding: EdgeInsets.symmetric(horizontal: 16),
      color: Colors.white,
      child: Row(
        children: [
          // Left buttons
          Row(
            children: [
              if (buttonShow) buildFooterButton("退单", Colors.black),
              SizedBox(width: 8),
              if (buttonShow) buildFooterButton("记录", Colors.black),
              SizedBox(width: 8),
              if (buttonShow && true) buildFooterButton("待分享", Colors.black), // Replace true with myChannel check
              SizedBox(width: 8),
              if (buttonShow) buildFooterButton("完善信息", Colors.black),
              SizedBox(width: 8),
              if (buttonShow) buildFooterButton("打电话", Color(0xFFFF4142)),
            ],
          ),
          Spacer(),
          // Right buttons
          Row(
            children: [
              if (buttonShow2) buildFooterButton("接单", Color(0xFFFF4142)),
              SizedBox(width: 8),
              if (buttonShow) buildFooterButtonWithStatus("委托协议", "待签署", Color(0xFFFF4142)),
              SizedBox(width: 8),
              if (buttonShow) buildFooterButtonWithStatus("发起定金合同", "待签署", Color(0xFFFF4142)),
              SizedBox(width: 8),
              if (buttonShow) buildFooterButtonWithStatus("缴纳定金", "待支付", Color(0xFFFF4142)),
              SizedBox(width: 8),
              if (buttonShow) buildFooterButtonWithStatus("发起合作合同", "待签署", Color(0xFFFF4142)),
              SizedBox(width: 8),
              if (buttonShow) buildFooterButtonWithStatus("服务费尾款", "待支付", Color(0xFFFF4142)),
              SizedBox(width: 8),
              if (buttonShow) buildFooterButtonWithStatus("代收薪资", "待支付", Color(0xFFFF4142)),
              SizedBox(width: 8),
              if (buttonShow) buildFooterButtonWithStatus("代收薪资", "本期付清", Color(0xFFFF4142)),
            ],
          ),
        ],
      ),
    );
  }

  Widget buildFooterButton(String text, Color color) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.circular(25),
        border: Border.all(color: color, width: 1),
      ),
      child: Text(
        text,
        style: TextStyle(fontSize: 14, color: Colors.white),
      ),
    );
  }

  Widget buildFooterButtonWithStatus(String mainText, String statusText, Color color) {
    return Column(
      children: [
        Container(
          padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
          decoration: BoxDecoration(
            color: color,
            borderRadius: BorderRadius.circular(25),
            border: Border.all(color: color, width: 1),
          ),
          child: Text(
            mainText,
            style: TextStyle(fontSize: 14, color: Colors.white),
          ),
        ),
        SizedBox(height: 4),
        Text(
          statusText,
          style: TextStyle(fontSize: 12, color: Colors.white),
        ),
      ],
    );
  }

  Widget buildWorkContent(String title, String content) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Color(0xFF191919)),
        ),
        SizedBox(height: 4),
        Text(
          content,
          style: TextStyle(fontSize: 14, color: Color(0xFF191919)),
        ),
      ],
    );
  }

  Widget buildTimeContent(String title, String content) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Color(0xFF191919)),
        ),
        SizedBox(height: 4),
        Text(
          content,
          style: TextStyle(fontSize: 14, color: Color(0xFF191919)),
        ),
      ],
    );
  }

  Widget buildInfoItem(String title, String content) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Color(0xFF191919)),
        ),
        SizedBox(height: 4),
        Text(
          content,
          style: TextStyle(fontSize: 14, color: Color(0xFF191919)),
        ),
      ],
    );
  }

  Widget buildPastInfoItem(String title, String content) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Image.asset('assets/appimg/add_btn.png', width: 20, height: 20),
            SizedBox(width: 5),
            Text(
              title,
              style: TextStyle(fontSize: 14, color: Color(0xFF191919)),
            ),
          ],
        ),
        SizedBox(height: 8),
        Container(
          padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
          decoration: BoxDecoration(
            color: Color(0xFFFFB7B9),
            borderRadius: BorderRadius.circular(3),
          ),
          child: Row(
            children: [
              SizedBox(width: 10),
              Text(
                content,
                style: TextStyle(fontSize: 14, color: Color(0xFF191919)),
              ),
            ],
          ),
        ),
      ],
    );
  }

  void onClickLeft() {
    // Handle back navigation
    Navigator.pop(context);
  }

  void onClickTab(int index) {
    setState(() {
      activeTab = index;
    });
  }

  void onClickCancel() {
    // Handle cancel action
    setState(() {
      isDialogVisible = false;
    });
  }

  void onClickConfirm() {
    // Handle confirm action
    setState(() {
      isDialogVisible = false;
    });
  }

  void clickJieDan() {
    // Handle accept order action
  }
}